package com.bm.specialvehiclefordispatcher.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import cn.jpush.android.api.JPushInterface;

import com.android.volley.VolleyError;
import com.bm.corelibs.logic.BaseLogic.NListener;
import com.bm.corelibs.utils.ToastMgr;
import com.bm.specialvehiclefordispatcher.R;
import com.bm.specialvehiclefordispatcher.Tools.Tools;
import com.bm.specialvehiclefordispatcher.activity.BaseActivity;
import com.bm.specialvehiclefordispatcher.activity.main.MainActivity;
import com.bm.specialvehiclefordispatcher.bean.BaseData;
import com.bm.specialvehiclefordispatcher.constants.Constant;
import com.bm.specialvehiclefordispatcher.logics.LoginManager;
import com.bm.specialvehiclefordispatcher.views.CommonNavigationBar;

public class SetPasswordActivity extends BaseActivity {

	CommonNavigationBar bar;
	EditText set_psw;
	EditText reset_psw;
	Button setpsw_sure;
	int type;
	String mobile;
	String tel;
	LoginManager manager = new LoginManager();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setpassword);
		findViews();
		init();
		addListeners();
	}

	@Override
	public void findViews() {
		type = getIntent().getIntExtra("type", -1);
		bar = (CommonNavigationBar) findViewById(R.id.navibar);
		set_psw = (EditText) findViewById(R.id.set_psw);
		reset_psw = (EditText) findViewById(R.id.reset_psw);
		setpsw_sure = (Button) findViewById(R.id.setpsw_sure);
	}

	@Override
	public void init() {
		tel=getIntent().getStringExtra("tel");
		mobile = getIntent().getStringExtra("mobile");
		bar.setListener(null);
		if (type == -1) {
			bar.setTitle("设置密码");
		} else {
			bar.setTitle("重置密码");
		}
	}

	@Override
	public void addListeners() {

		setpsw_sure.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (Tools.getTextViewText(set_psw).equals(Tools.getTextViewText(reset_psw))) {

					if (Tools.getTextViewText(set_psw).length() >= 6 && Tools.getTextViewText(reset_psw).length() >= 6) {

						if (type == -1) {
							manager.register(mobile, Tools.getTextViewText(set_psw), registerListener());
						} else {
							// 重置密码
							loadingDialog.show();
							manager.findBackPassword(tel, Tools.getTextViewText(reset_psw), Constant.ROLE, findBackPassword);
						}
					} else {
						showToast("请输入6位以上密码");
					}

				} else {
					showToast("两次输入不一致");
				}

			}
		});
	}
	
	private NListener<BaseData> registerListener() {
		return new NListener<BaseData>() {
			@Override public void onResponse(BaseData data) {
				loadingDialog.dismiss();
				if (data.status == Constant.STATUS_OK) {
					ToastMgr.show("注册成功");
					if(data.data.Dispatcher != null)
						LoginManager.getInstance(mActivity).saveUserInfo(data.data.Dispatcher);
					JPushInterface.setAlias(mActivity,
							data.data.Dispatcher.id+"", null);
					startActivity(new Intent(SetPasswordActivity.this, MainActivity.class));
				} else {
					ToastMgr.show(data.msg);
				}
			}

			@Override public void onErrResponse(VolleyError error) {
				loadingDialog.dismiss();
			}
			@Override public void onAllPageLoaded(int nowPage, int totalPage) {}
		};
	}
	
	NListener<BaseData> findBackPassword =new NListener<BaseData>() {

		@Override
		public void onResponse(BaseData data) {
			loadingDialog.dismiss();
			if (data.status==Constant.STATUS_OK) {
				startActivity(new Intent(SetPasswordActivity.this, LoginActivity.class));
			}
			
		}

		@Override
		public void onErrResponse(VolleyError error) {
			loadingDialog.dismiss();
		}

		@Override
		public void onAllPageLoaded(int nowPage, int totalPage) {
			
		}
	};

}
